<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="病人姓名" prop="patientId"
                :rules="[
                                              {required: true, message: '病人姓名不能为空', trigger: 'blur'}

                 ]">
                <patient-select :limit='1' :value="inputForm.patientId" @getValue='(value) => {inputForm.patientId=value}'></patient-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预约科室" prop="departmentId"
                        :rules="[
                          {required: true, message: '预约科室不能为空', trigger: 'blur'}
                 ]">
            <SelectTree
              ref="departmentId"
              :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

              url="/sys/office/treeData?type=2"
              :value="inputForm.departmentId"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {inputForm.departmentId=value}"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="医生姓名" prop="doctorId"
                :rules="[
                  {required: true, message: '医生姓名不能为空', trigger: 'blur'}
                 ]">
                <doctor-select :limit='1' :value="inputForm.doctorId" @getValue='(value) => {inputForm.doctorId=value}'></doctor-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="预约时间" prop="appointmentTime"
                :rules="[
                  {required: true, message: '预约时间不能为空', trigger: 'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.appointmentTime"
                      type="datetime"
                      :picker-options="pickerOptions"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>

        <!--<el-col :span="12">
            <el-form-item label="预约状态" prop="appointmentStatus"
                :rules="[
                 ]">
                <el-select v-model="inputForm.appointmentStatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('application_state')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>-->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import doctorSelect from '@/components/doctorSelect'
  import patientSelect from '@/components/patientSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import appointmentInfoService from '@/api/appointment/appointmentInfoService'
  export default {
    data () {
      return {
        pickerOptions: {
          disabledDate (time) {
            return time.getTime() < Date.now() - 8.64e7
          }
        },
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          patientId: '',
          doctorId: '',
          appointmentTime: '',
          departmentId: '',
          appointmentStatus: '2'
        }
      }
    },
    components: {
      doctorSelect,
      patientSelect,
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建预约信息`
        } else if (method === 'edit') {
          this.title = '修改预约信息'
        } else if (method === 'view') {
          this.title = '查看预约信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            appointmentInfoService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            appointmentInfoService.save(this.inputForm).then(({data}) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>


